package com.mobilesalon.tools;

import android.database.Cursor;

public class CursorToArray {

	public static final String[][] parse(Cursor cursor, boolean isIncludeHeader) {
		if (cursor == null || cursor.getCount() == 0) {
			return new String[0][0];
		}

		String[] columns = cursor.getColumnNames();

		int i = 0;
		String[][] array;
		if (isIncludeHeader) {
			array = new String[cursor.getCount() + 1][columns.length];
			array[i] = columns;
			i++;
		} else {
			array = new String[cursor.getCount()][columns.length];
		}

		while (cursor.moveToNext()) {
			for (int j = 0; j < columns.length; j++) {
				array[i][j] = cursor.getString(j);
			}
			i++;
		}

		cursor.moveToFirst();

		return array;

	}
	
	public static final String parseToString(Cursor cursor) {
		if (cursor == null || cursor.getCount() == 0) {
			return "";
		}

		
		String[] columns = cursor.getColumnNames();

		int i = 0;
		StringBuilder sb =  new StringBuilder();


		while (cursor.moveToNext()) {
			for (int j = 0; j < columns.length; j++) {
				sb.append(columns[j]).append('=').append(cursor.getString(j)).append("\n");
			}
			i++;
		}

		cursor.requery();

		return sb.toString();

	}
	

}
